package me.yricky.cbor

fun ByteArray.hexString() = map { String.format("%02x",it) }.let {
    val sb = StringBuilder()
    it.forEach { s ->
        sb.append(s).append(" ")
    }
    sb.toString()
}

fun CBORKv.unwrapStringMap():Pair<Map<String,CBORItem>,List<Pair<CBORItem,CBORItem>>> {
    val map  = mutableMapOf<String,CBORItem>()
    val list = mutableListOf<Pair<CBORItem,CBORItem>>()
    value.forEach {
        if(it.first is CBORString){
            val str = (it.first as CBORString).value
            if(map[str] == null){
                map[str] = it.second
            }else{
                list.add(it)
            }
        }else{
            list.add(it)
        }
    }
    return Pair(map,list)
}

fun CBORKv.unwrapNumberMap():Pair<Map<Long,CBORItem>,List<Pair<CBORItem,CBORItem>>> {
    val map  = mutableMapOf<Long,CBORItem>()
    val list = mutableListOf<Pair<CBORItem,CBORItem>>()
    value.forEach {
        if(it.first is CBORNum){
            val num = (it.first as CBORNum).value
            if(map[num] == null){
                map[num] = it.second
            }else{
                list.add(it)
            }
        }else{
            list.add(it)
        }
    }
    return Pair(map,list)
}
